<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\model\ZconfigModel;
use app\portal\model\ZhanModel;
use app\portal\model\ZjlModel;
use app\portal\model\ZuiModel;
use cmf\controller\AdminBaseController;
use Symfony\Component\Yaml\Tests\DumperTest;
use Think\Db;

class AdminLiController extends AdminBaseController
{

    public $num= 1000000;
    /** 商品记录
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {

        $arr = $arr_c = [];

        $param  = $this->request->param('keyword');

        if(!empty($param)){
            $arr = ['name'=>$param];
        }

        $ZliMode = new ZhanModel();

        $list = $ZliMode->lists($arr);

        $this->assign('keyword', $param);//搜索条件
        $this->assign('data',  $list->toArray()['data'] );
        $list->appends($param);//分页保留参数
        $this->assign('page', $list->render());//分页

        return $this->fetch();
    }

    /** 购买理财
     * @return mixed
     */
    public function li()
    {

        $arr = $arr_c = [];
        $ZliMode = new ZconfigModel();
        $list = $ZliMode->lists($arr);
        $this->assign('data',  $list->toArray()['data'] );

        return $this->fetch();
    }
    //添加用户
    public function add_user()
    {
        $Category = new ZhanModel();
        if($this->request->isPost()) {

            $where = [];
            $param = $this->request->param();

            if (empty($param['name'])) { $this->error('用户名不能为空', ''); }

            $arr = [
                'name' => $param['name'],
            ];
            $wh = [ 'name' => $param['name']];

            if (!empty($param['id'])) {

                $wh['id'] = ['NEQ',$param['id']] ;
                $refindus = $Category->where($wh)->find();
                if($refindus){  $this->error('用户名已存在', ''); }

                $where['id'] = $param['id'];

                $Category->where($where)->update($arr);

            } else {
                $refindus = $Category->where($wh)->find();
                if($refindus){  $this->error('用户名已存在', ''); }


                $arr['create_time'] = date('Y-m-d H:i:s');

                $Category->insert($arr);
            }

            $this->success('操作成功', url('AdminLi/index'));

        }else{
            $data = [];
            $id = $this->request->param('id');
            if(!empty($id)){
                $data = $Category->where(['id'=>$id])->find();
            }

            $this->assign('data', $data);//搜索条件

            return $this->fetch();
        }

    }

    //添加理财
    public function add()
    {
        $Category = new ZconfigModel();
        if($this->request->isPost()) {

            $arr = $where = [];
            $param = $this->request->param();

            if (empty($param['name'])) {  $this->error('名字不能为空', ''); }
            if (empty($param['day'])) {  $this->error('每日收益百分比不能为空', ''); }
            $arr = [
                'name' => $param['name'],
                'day' => $param['day']*$this->num,
            ];

            if (!empty($param['id'])) {
                $wh = [ 'name' => $param['name']];
                $wh['id'] = ['NEQ',$param['id']] ;
                $refindus = $Category->where($wh)->find();
                if($refindus){  $this->error('名字已存在', ''); }
                $where['id'] = $param['id'];

                $res = $Category->where($where)->update($arr);

            } else {
                $refindus = $Category->where([ 'name' => $param['name']])->find();
                if($refindus){  $this->error('名字已存在', ''); }
                $arr['create_time'] = date('Y-m-d H:i:s');
                $res = $Category->insert($arr);
            }

              $this->success('操作成功', url('AdminLi/li'));

        }else{
            $data = [];
            $id = $this->request->param('id');
            if(!empty($id)){
                $data = $Category->where(['id'=>$id])->find();
            }

            $this->assign('data', $data);//搜索条件

            return $this->fetch();
        }

    }

    /** 我的购买列表
     * @return mixed
     */
    public function my_list(){
        $arr = $arr_c =  [];

        $param  = $this->request->param('id');
        if(empty($param)){   $this->error('操作失败', '');   }
        session('u_id',$param);
        if($this->request->isPost()) {
            $param = $this->request->param();
        }
        $ZliMode = new ZuiModel();

        $list = $ZliMode->lists(['u_id'=>$param]);

        $this->assign('data',  $list->toArray()['data'] );
        $list->appends($param);//分页保留参数
        $this->assign('page', $list->render());//分页

        return $this->fetch();
    }

    //我的收益记录
    public function my_zui(){
        $this->z_time();
        $arr = $arr_c =  [];

        $param  = $this->request->param('id');
        if(empty($param)){   $this->error('操作失败', '');   }
        session('u_id',$param);
        if($this->request->isPost()) {
            $param = $this->request->param();
        }

        $ZliMode = new ZjlModel();

        $list = $ZliMode->lists(['u_id'=>$param]);

        $this->assign('keyword', $param);//搜索条件
        $this->assign('data',  $list->toArray()['data'] );
        $list->appends($param);//分页保留参数
        $this->assign('page', $list->render());//分页

        return $this->fetch();
    }

    //提交购买理财
    public function my_li(){
        $result = false;
        $u_id = session('u_id');
        if (!empty($u_id)) {


            if ($this->request->isPost()) {

                $param = $this->request->param();
                $id = intval($this->request->param('id'));
                if (empty($param['number'])) {
                    $this->error('请输入正确的购买金额', '');
                }
                $re = (new ZconfigModel())->where(['id' => $id])->find();

                if ($re) {

                    $arr_ = [
                        'u_id' => $u_id,
                        'c_id' => $id,
                        'price' => $param['number'] * $this->num,

                        'create_time' => date('Y-m-d H:i:s '),
                    ];

                    Db::startTrans();
                    try {

                        //购买表
                        (new ZuiModel())->insert($arr_);
                        //用户表
                        (new ZhanModel())->where(['id' => $u_id])->setInc('price', $param['number'] * $this->num);

                        $result = true;

                        // 提交事务
                        Db::commit();
                    } catch (\Exception $e) {
                        // 回滚事务
                        Db::rollback();
                        $result = false;

                    }
                }
                if ($result) {
                    $this->success('购买成功', url('AdminLi/my_list', ['id' =>$u_id]));
                }
            }
        }


        $this->error('购买失败', '');

    }


    //定时收益（）
    public function z_time(){
        $ZjlModel = new ZjlModel();
        $date = date('Y-m-d');
        $star = "$date 00:00";
        $end = "$date 23:59";

        $data = new ZuiModel();
        $list = $data->alias('u')->join('zconfig','u.c_id=zconfig.id')
        ->field('u.price,u.u_id,u.id,zconfig.day')->select()->toArray();


        $ZhanModel = new ZhanModel();

        foreach ($list as $val) {
            Db::startTrans();
            try {
                $where = ['u_id' => $val['u_id'], 'type' => 0, 'z_id' => $val['id']];
                $where['create_time'] = ['between', [$star, $end]];
                $su = $ZjlModel->where($where)->find();
                if ($su) {
                    continue;
                }

                $price = ($val['price'] / $this->num) * ($val['day'] / 100000000);

                $price = $price * $this->num;

                $arr = [
                    'u_id' => $val['u_id'],
                    'z_id' => $val['id'],
                    'price' => $price,
                    'type' => 0,//0-利息  1-购买理财
                    'create_time' => date('Y-m-d H:i:s '),
                ];
                //记录表
                $ZjlModel->insert($arr);

                //用户表
                $ar = $ZhanModel->field('price_l')->where(['id' => $val['u_id']])->find();
                $total = $ar['price_l'] + $price;
                $att = [//30000000
                    'price_l' => $total,//利息总计
                ];
              // dump($att);
                $ZhanModel->where(['id' => $val['u_id']])->update($att);

                Db::rollback();
                // 提交事务
                //   Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }
        }

    }


}